//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package javax.vpncrypto;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.io.Serializable;
import java.io.ObjectInputStream.GetField;
import java.io.ObjectOutputStream.PutField;
import java.security.Permission;
import java.security.PermissionCollection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import javax.vpncrypto.CryptoPolicyParser.ParsingException;

final class CryptoPermissions extends PermissionCollection implements Serializable {
    private static final long serialVersionUID = 4946547168093391015L;
    private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[]{new ObjectStreamField("perms", Hashtable.class)};
    private transient ConcurrentHashMap<String, PermissionCollection> perms = new ConcurrentHashMap(7);

    CryptoPermissions() {
    }

    void load(InputStream var1) throws IOException, ParsingException {
        javax.vpncrypto.CryptoPolicyParser var2 = new javax.vpncrypto.CryptoPolicyParser();
        var2.read(new BufferedReader(new InputStreamReader(var1, "UTF-8")));
        javax.vpncrypto.CryptoPermission[] var3 = var2.getPermissions();

        for(int var4 = 0; var4 < var3.length; ++var4) {
            this.add(var3[var4]);
        }

    }

    boolean isEmpty() {
        return this.perms.isEmpty();
    }

    public void add(Permission var1) {
        if (this.isReadOnly()) {
            throw new SecurityException("Attempt to add a Permission to a readonly CryptoPermissions object");
        } else if (var1 instanceof javax.vpncrypto.CryptoPermission) {
            javax.vpncrypto.CryptoPermission var2 = (javax.vpncrypto.CryptoPermission)var1;
            PermissionCollection var3 = this.getPermissionCollection(var2);
            var3.add(var2);
            String var4 = var2.getAlgorithm();
            this.perms.putIfAbsent(var4, var3);
        }
    }

    public boolean implies(Permission var1) {
        if (!(var1 instanceof javax.vpncrypto.CryptoPermission)) {
            return false;
        } else {
            javax.vpncrypto.CryptoPermission var2 = (javax.vpncrypto.CryptoPermission)var1;
            PermissionCollection var3 = this.getPermissionCollection(var2.getAlgorithm());
            return var3.implies(var2);
        }
    }

    public Enumeration<Permission> elements() {
        return new javax.vpncrypto.PermissionsEnumerator(this.perms.elements());
    }

    CryptoPermissions getMinimum(CryptoPermissions var1) {
        if (var1 == null) {
            return null;
        } else if (this.perms.containsKey("CryptoAllPermission")) {
            return var1;
        } else if (var1.perms.containsKey("CryptoAllPermission")) {
            return this;
        } else {
            CryptoPermissions var2 = new CryptoPermissions();
            PermissionCollection var3 = (PermissionCollection)var1.perms.get("*");
            int var4 = 0;
            if (var3 != null) {
                var4 = ((javax.vpncrypto.CryptoPermission)var3.elements().nextElement()).getMaxKeySize();
            }

            Enumeration var5 = this.perms.keys();

            while(true) {
                javax.vpncrypto.CryptoPermission[] var9;
                while(true) {
                    if (!var5.hasMoreElements()) {
                        PermissionCollection var12 = (PermissionCollection)this.perms.get("*");
                        if (var12 == null) {
                            return var2;
                        }

                        var4 = ((javax.vpncrypto.CryptoPermission)var12.elements().nextElement()).getMaxKeySize();
                        Enumeration var13 = var1.perms.keys();

                        while(true) {
                            String var14;
                            do {
                                if (!var13.hasMoreElements()) {
                                    return var2;
                                }

                                var14 = (String)var13.nextElement();
                            } while(this.perms.containsKey(var14));

                            PermissionCollection var15 = (PermissionCollection)var1.perms.get(var14);
                            javax.vpncrypto.CryptoPermission[] var16 = this.getMinimum(var4, var15);

                            for(int var11 = 0; var11 < var16.length; ++var11) {
                                var2.add(var16[var11]);
                            }
                        }
                    }

                    String var6 = (String)var5.nextElement();
                    PermissionCollection var7 = (PermissionCollection)this.perms.get(var6);
                    PermissionCollection var8 = (PermissionCollection)var1.perms.get(var6);
                    if (var8 == null) {
                        if (var3 == null) {
                            continue;
                        }

                        var9 = this.getMinimum(var4, var7);
                        break;
                    }

                    var9 = this.getMinimum(var7, var8);
                    break;
                }

                for(int var10 = 0; var10 < var9.length; ++var10) {
                    var2.add(var9[var10]);
                }
            }
        }
    }

    private javax.vpncrypto.CryptoPermission[] getMinimum(PermissionCollection var1, PermissionCollection var2) {
        Vector var3 = new Vector(2);
        Enumeration var4 = var1.elements();

        while(true) {
            while(var4.hasMoreElements()) {
                javax.vpncrypto.CryptoPermission var5 = (javax.vpncrypto.CryptoPermission)var4.nextElement();
                Enumeration var6 = var2.elements();

                while(var6.hasMoreElements()) {
                    javax.vpncrypto.CryptoPermission var7 = (javax.vpncrypto.CryptoPermission)var6.nextElement();
                    if (var7.implies(var5)) {
                        var3.addElement(var5);
                        break;
                    }

                    if (var5.implies(var7)) {
                        var3.addElement(var7);
                    }
                }
            }

            javax.vpncrypto.CryptoPermission[] var8 = new javax.vpncrypto.CryptoPermission[var3.size()];
            var3.copyInto(var8);
            return var8;
        }
    }

    private javax.vpncrypto.CryptoPermission[] getMinimum(int var1, PermissionCollection var2) {
        Vector var3 = new Vector(1);
        Enumeration var4 = var2.elements();

        while(var4.hasMoreElements()) {
            javax.vpncrypto.CryptoPermission var5 = (javax.vpncrypto.CryptoPermission)var4.nextElement();
            if (var5.getMaxKeySize() <= var1) {
                var3.addElement(var5);
            } else if (var5.getCheckParam()) {
                var3.addElement(new javax.vpncrypto.CryptoPermission(var5.getAlgorithm(), var1, var5.getAlgorithmParameterSpec(), var5.getExemptionMechanism()));
            } else {
                var3.addElement(new javax.vpncrypto.CryptoPermission(var5.getAlgorithm(), var1, var5.getExemptionMechanism()));
            }
        }

        javax.vpncrypto.CryptoPermission[] var6 = new javax.vpncrypto.CryptoPermission[var3.size()];
        var3.copyInto(var6);
        return var6;
    }

    PermissionCollection getPermissionCollection(String var1) {
        PermissionCollection var2 = (PermissionCollection)this.perms.get("CryptoAllPermission");
        if (var2 == null) {
            var2 = (PermissionCollection)this.perms.get(var1);
            if (var2 == null) {
                var2 = (PermissionCollection)this.perms.get("*");
            }
        }

        return var2;
    }

    private PermissionCollection getPermissionCollection(javax.vpncrypto.CryptoPermission var1) {
        String var2 = var1.getAlgorithm();
        PermissionCollection var3 = (PermissionCollection)this.perms.get(var2);
        if (var3 == null) {
            var3 = var1.newPermissionCollection();
        }

        return var3;
    }

    private void readObject(ObjectInputStream var1) throws IOException, ClassNotFoundException {
        GetField var2 = var1.readFields();
        Hashtable var3 = (Hashtable)((Hashtable)var2.get("perms", (Object)null));
        if (var3 != null) {
            this.perms = new ConcurrentHashMap(var3);
        } else {
            this.perms = new ConcurrentHashMap();
        }

    }

    private void writeObject(ObjectOutputStream var1) throws IOException {
        Hashtable var2 = new Hashtable(this.perms);
        PutField var3 = var1.putFields();
        var3.put("perms", var2);
        var1.writeFields();
    }
}
